<?php
namespace app\index\controller;
use think\Cache;
use think\cache\driver\Redis;
use think\Db;


function hashSet($redis,$key,$data) {
    foreach ($data as $k=>$v) {
        if($redis->hset($key,$k,$v)==false) {
            return false;
        }
    }
    return true;
}



class Note {
            //通过用户添加记事


    public function add() {
        //return "what the fox say!!!";

        $redis = new \Redis();
        $redis->connect("127.0.0.1","6379");

        $userName = $_POST["user_name"];
        $noteName = $_POST["note_name"];
        $noteText = $_POST["note_text"];
        $noteDatetime = date("Y-m-d H:i:s",time());
        $data = [
            "user_name" => $userName,
            "note_name" => $noteName,
            "note_text" => $noteText,
            "note_datetime" => $noteDatetime,
        ];

        //不允许同一个用户下记事本名称的重复
        $nameData = [
            "note_name"=>$data,
        ];
        //echo "before insert";
        if($redis->exists("hash-".$noteName)==true) {
            return "failed,note name exist!";
        }

        if(hashSet($redis,"hash-".$noteName,$data)==false) {
        //if($redis->hset($noteName,$nameData)==false) {
            return "failed to insert into redis";
        }

        $endata = json_encode($data);
        if($redis->lPush("list-".$userName,$endata)==false) {
            return "failed to insert into redis";
        }
        return "success";
    }

    public function addfile() {

        $redis = new \Redis();
        $redis->connect("127.0.0.1","6379");

        $userName = $_POST["user_name"];
        $noteName = explode(".",$_FILES["file"]["name"])[0];
        $noteDatetime = date("Y-m-d H:i:s",time());
        $noteText = file_get_contents($_FILES["file"]["tmp_name"]);
        $encode = mb_detect_encoding($noteText, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
        $noteText = mb_convert_encoding($noteText, 'UTF-8',$encode );

        //不允许记事本名称重复
        if($redis->exists("hash-".$noteName)==true) {
            return "failed,note name exist!";
        }

        $data = [
            "note_name" => $noteName,
            "note_text" => $noteText,
            "user_name" => $userName,
            "note_datetime" => $noteDatetime,
        ];

        if(hashset($redis,"hash-".$noteName,$data)==false) {
            return "failed insert into redis";
        }
        if($redis->lPush("list-".$userName,json_encode($data))==false) {
            return "failed insert into redis";
        }
        return "success";
    }

    //通过用户名和noteName获取note，得到某位用户的某个记事
    public function get() {
        $redis = new \Redis();
        $redis->connect("127.0.0.1","6379");

        $userName = $_POST["user_name"];
        $noteName = $_POST["note_name"];

        $data = [
            "text" => $redis->hget("hash-".$noteName,"note_text"),
            "time" => $redis->hget("hash-".$noteName,"note_datetime"),
        ];
        echo "time :\r\n";
        echo $data["time"]."\r\n";
        echo "text :\r\n";
        echo $data["text"]."\r\n";
    }

    public function test() {
        echo "hahahhah";
        $userName = $_POST["user_name"];
        $noteName = $_POST["note_name"];
        $noteText = $_POST["note_text"];
        echo $userName." ".$noteName." ".$noteText;
    }

    //得到某位用户的所有记事
    public function getAll() {
        //return "eeee";
                $redis = new \Redis;
                $redis->connect("127.0.0.1","6379");
                $userName = $_POST["user_name"];
                $len = $redis->lLen("list-".$userName);
                $listdata = $redis->lRange("list-".$userName,0,$len);
                if($listdata==null) {
                    return "nulllll";
                }
                foreach ($listdata as $v) {
                    $dedata = json_decode($v);
                    //echo $dedata["note_text"];
                   // echo $v;
                    //echo $v["note_text"];
                    foreach ($dedata as $key=>$value) {
                        echo $key." : ";
                        echo $value.PHP_EOL;
                    }
                    echo PHP_EOL;
                }
                //echo "user : ".$userName."\n";

    }


}